<?php

namespace app\store\controller;

use think\Session;
use wxpay\example\Jsapi;
use app\store\service\WxService as WxService;
use think\Db;
use app\store\model\Goods as GoodsModel;
use app\api\model\Coupon as CouponModel;
use app\api\controller\Goods;
/**
 * 商户认证
 * Class Passport
 * @package app\store\controller
 */
class Mobile extends \think\Controller
{
    /**
     * 商户后台登录
     * @return array|bool|mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    //飞就阅看
    const APP_ID = 'wx21d45bb64c0cd6a7';
    const APP_SECRET = 'ddb04624249ff50d2975ce7d9688a299';
    const TEMPLATE_ID = 'NJO-6pxj7dgEatOAM7_TruUfvXRJ8iP9CDxGEb0BuTA';

    public function index()
    {
        error_reporting(1);
        header('Content-type:text/html; Charset=utf-8');
        $appid = self::APP_ID; //微信公众平台->开发->基本配置->AppID
        $appKey = self::APP_SECRET;  //微信公众平台->开发->基本配置->AppSecret
        //①、获取用户openid
        $wxPay = new WxService($appid, $appKey);

        $openid = session('openid');
        $member = Db::name('user')->where('open_id',$openid)->select()->toArray();

        if(empty($openid)||empty($member)){
            $data = $wxPay->getOathuOpenid();      //获取openid
            if (!$data['openid']) {
                exit('获取openid失败');
            }
            $_SESSION['openid'] = $data['openid'];
            session('openid',$data['openid']);
            // die();
            //②、获取用户信息
            $user = $wxPay->getUserInfo($data['openid'], $data['access_token']);
            session('nickname',$user['nickname']);
            session('headimgurl',$user['headimgurl']);
           // var_dump($user);die();
            if(!empty($member['openid'])){
                $member = Db::name('user')->insert(array('open_id'=>$data['openid'],'nickName'=>$user['nickname'],'avatarUrl'=>$user['headimgurl'],'create_time'=>time(),'update_time'=>time()));

            }
        }
        $ssid = input("get.ssid");

        $device = GoodsModel::detail($ssid)->toArray();
       // var_dump($device);die();
        $this->view->engine->layout(false);
        return $this->fetch('index', [
            // 系统版本号
            'device' => $device
        ]);

    }
    public function pay(){
       $pay = new Jsapi();
       $openId="o292W1Nd32tB6RhYJbOQPOLtnudk";
       $body ="测试支付";
       $order_num = time();
       $total = 100;
       $pay->pay($openId, $body, $order_num, $total);
    }
    public function Coupon(){

        $model = new CouponModel;
        $list = $model->getList();
        return json($list);
    }
    public function order(){

        $openid = session('openid');
        $member = Db::name('user')->where('open_id',$openid)->find();
        $money = input("post.money");
        $data = array(
            'order_no'=>time(),
            'total_price'=>$money,
            'order_price'=>$money,
            'coupon_id'=>intval(input('post.coupon_id')),
            'wxapp_id'=>1001,
            'create_time'=>time(),
            'update_time'=>time(),
            'user_id'=>$member['user_id']
        );
        $orderid =  Db::name('order')->insertGetId($data);
        //插入设备订单表
        $ssid = input("post.ssid");

        $device = GoodsModel::detail($ssid)->toArray();
        $good_data = array(
            'goods_id'=>$ssid,
            'goods_name'=>$device['goods_name'],
            'total_num'=>1,
            'order_id'=>$orderid,
            'wxapp_id'=>10001,
            'create_time'=>time(),
            'total_price'=>$money,
            'user_id'=>$member['user_id']
        );
        $orderid =  Db::name('order_goods')->insertGetId($good_data);
        return json(array('code'=>200,'orderid'=>$orderid));
    }
    /**
     * 退出登录
     */
    public function logout()
    {
        Session::clear('yoshop_store');
        $this->redirect('passport/login');
    }

}
